<?php

namespace App\Http\Controllers;

use App\Models\Business;
use App\Models\Department;
use App\Models\Record;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Resources\Record as RecordResource;
use App\Http\Resources\RecordCollection;
use Illuminate\Support\Facades\Cache;

class RecordController extends Controller
{
    public function overall(Request $request)
    {
        $department_id = $request->department_id;
        $department_ids = Department::getAllChildrenId($department_id);

        $data_cycle = data_cycles_from_date($request->date);

        if ($data_cycle['month']->greaterThan(Carbon::now())){
            abort(404, '日期错误');
        }

        $monthIds = Business::getBusinessIdByDataCycle('月');
        $quarterIds = Business::getBusinessIdByDataCycle('季度');
        $halfYearIds = Business::getBusinessIdByDataCycle('半年');
        $yearIds = Business::getBusinessIdByDataCycle('年');

        $monthRecords = Record::whereIn('department_id',$department_ids)->whereIn('business_id',$monthIds)->whereDate('record_date',$data_cycle['month'])->get();
        $quarterRecords = Record::whereIn('department_id',$department_ids)->whereIn('business_id',$quarterIds)->whereDate('record_date',$data_cycle['quarter'])->get();
        $halfYearRecords = Record::whereIn('department_id',$department_ids)->whereIn('business_id',$halfYearIds)->whereDate('record_date',$data_cycle['half_year'])->get();
        $yearRecords = Record::whereIn('department_id',$department_ids)->whereIn('business_id',$yearIds)->whereDate('record_date',$data_cycle['year'])->get();
        $records = $monthRecords->merge($quarterRecords)->merge($halfYearRecords)->merge($yearRecords);

        return RecordResource::collection($records);
    }
    //
    public function statistics(Request $request)
    {
        $department_id = $request->department_id ; //默认显示芒市支行
        $department_ids = Department::getAllChildrenId($department_id);

        $statistic_array = array();

        $statistic_object = $this->getStatisticsByType($department_ids, '优秀');
        array_push($statistic_array,$statistic_object);
        $statistic_object = $this->getStatisticsByType($department_ids, '良好');
        array_push($statistic_array,$statistic_object);
        $statistic_object = $this->getStatisticsByType($department_ids, '一般');
        array_push($statistic_array,$statistic_object);
        $statistic_object = $this->getStatisticsByType($department_ids, '关注');
        array_push($statistic_array,$statistic_object);
        $statistic_object = $this->getStatisticsByType($department_ids, '重点关注');
        array_push($statistic_array,$statistic_object);

        return $statistic_array;
    }

    public function department(Request $request){

    }

    public function getStatisticsByType($department_ids,$type)
    {
        $count = DB::table('records')->whereIn('department_id', $department_ids)->where('evaluation_result', '=', $type)->count();
        $object = array(['type' => $type, 'count' => $count]);
        return $object;
    }
}
